package com.jxau.mall.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxau.mall.user.entity.UserLoginLog;
import com.jxau.mall.user.mapper.UserLoginLogMapper;
import com.jxau.mall.user.model.UserLoginLogQuery;
import com.jxau.mall.user.service.IUserLoginLogService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: shop
 * @description: user_login_log表业务层接口的实现类
 * @author: guojiani
 * @create: 2025-04-24 14:41
 **/
@Service
public class UserLoginLogServiceImpl extends ServiceImpl<UserLoginLogMapper, UserLoginLog> implements IUserLoginLogService {
    @Override
    public List<UserLoginLog> list(UserLoginLogQuery query) {
        // 1 创建链式条件查询器，在Mapper层构建查询条件
        LambdaQueryChainWrapper<UserLoginLog> queryWrapper = new LambdaQueryChainWrapper<>(baseMapper);
        // 2 构建条件
        // 2.1 如果userId不为空，按userId查询
        if(query.getUserId() != null){
            queryWrapper.eq(UserLoginLog::getUserId,query.getUserId());
        }
        // 2.2 如果起始时间和终止时间不为空
        if(query.getStartTime() != null && query.getStopTime() != null){
            queryWrapper.between(UserLoginLog::getCreateTime,query.getStartTime(),query.getStopTime());
        }else if(query.getStartTime() != null) { // 如果起始时间不为空
            queryWrapper.ge(UserLoginLog::getCreateTime,query.getStartTime());
        }else if(query.getStopTime() != null){ // 如果终止时间不为空
            queryWrapper.le(UserLoginLog::getCreateTime,query.getStopTime());
        }
        return queryWrapper.list();
    }

    @Override
    public IPage<UserLoginLog> page(UserLoginLogQuery query, Page page) {
        LambdaQueryChainWrapper<UserLoginLog>  queryWrapper = new LambdaQueryChainWrapper<>(baseMapper);
        // 2 构建条件
        // 2.1 如果userId不为空，按userId查询
        if(query.getUserId() != null){
            queryWrapper.eq(UserLoginLog::getUserId,query.getUserId());
        }
        // 2.2 如果起始时间和终止时间不为空
        if(query.getStartTime() != null && query.getStopTime() != null){
            queryWrapper.between(UserLoginLog::getCreateTime,query.getStartTime(),query.getStopTime());
        }else if(query.getStartTime() != null) { // 如果起始时间不为空
            queryWrapper.ge(UserLoginLog::getCreateTime,query.getStartTime());
        }else if(query.getStopTime() != null){ // 如果终止时间不为空
            queryWrapper.le(UserLoginLog::getCreateTime,query.getStopTime());
        }
        return queryWrapper.page(page);
    }


}
